package com.tangdi.mkfile.gzdwhz;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.RectangleReadOnly;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.tangdi.ReportFileInfo;
import com.tangdi.Utils;
import com.tangdi.Bean.CZGZ_DWXX;
import com.tangdi.Bean.CZGZ_DWXX_TOTAL;
import com.tangdi.Dao.impl.GongziDWImpl;
import com.tangdi.Dao.impl.GongziMXImpl;

public class CreateDWGZPDF_LISTALL {
	//private Logger logger = Logger.getLogger(CreateDWGZPDF_LISTALL.class);
	private String pdf_url_root = Utils.getFilePath("root_path");
	private String folder_Name = ReportFileInfo.FOLDER_NAME_GZDWHZ.getName();
	private String file_Type = "pdf";
	private String file_Name = ReportFileInfo.FILE_NAME_GZDWHZ.getName();
	private Font FontChinese;
	private String pageTitle;
	private String pageHeader;
	public int createPDF(List<CZGZ_DWXX> listDW, CZGZ_DWXX_TOTAL dwCount, String month) {
		float totalWidths = 530;
		float[] cellWidths = {140, 80, 80, 30, 50, 50, 50, 50};
		
	 //ByteArrayOutputStream baos = new ByteArrayOutputStream();
	 try {
		 BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		 //BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
		 FontChinese = new Font(bfChinese, 7, Font.NORMAL);
		 String pdf_url = pdf_url_root + "/" + month + "/" + file_Type + "/" + folder_Name +"/" + month + "_" + file_Name + ".pdf";
				//在填写文件路径时，一定要写上具体的文件名称（xx.txt），否则会出现拒绝访问。
				File file = new File(pdf_url);
				if(!file.exists()){
				 //先得到文件的上级目录，并创建上级目录，在创建文件
					file.getParentFile().mkdirs();
					file.createNewFile();
				}
				//设置页面大小
		 Document document = new Document(new RectangleReadOnly(595F, 920F));//不设置页面大小 在footer中设置 setFooter
		 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdf_url));
				//PdfWriter.getInstance(document, baos);
		 
		 pageHeader = "单位总数：" + dwCount.getDWCOUNT().trim();
		 pageTitle = dwCount.getYF().substring(0, 4) + "年" + dwCount.getYF().substring(4, 6) + "月单位工资发放汇总表";
				
		 setFooter(writer,bfChinese);
	 
			document.open();
			
			PdfPTable table = new PdfPTable(cellWidths);

			table.setHeaderRows(1);//设置表头，分页展示
			//table.setSpacingBefore(80f);
			table.setTotalWidth(totalWidths);
			table.setLockedWidth(true);
			
			table.addCell(getCell("单位名称", 1, 1));
			table.addCell(getCell("代扣款银行", 1, 1));
			table.addCell(getCell("代扣款银行账号", 1, 1));
			table.addCell(getCell("人数", 1, 1));
			table.addCell(getCell("应发", 1, 1));
			table.addCell(getCell("扣款", 1, 1));
			table.addCell(getCell("实发", 1, 1));
			table.addCell(getCell("个人所得税", 1, 1));
			
			GongziMXImpl gzmx = new GongziMXImpl();
			String geShui_Count = "0";
			
			table.addCell(getCell_Left("合    计", 1, 1));
			table.addCell(getCell("", 1, 1));
			table.addCell(getCell("", 1, 1));
			table.addCell(getCell_Right(dwCount.getTF_RS_TOTAL(), 1, 1));
			table.addCell(getCell_Right(dwCount.getYF_GZ_TOTAL(), 1, 1));
			table.addCell(getCell_Right(dwCount.getDF_GZ_TOTAL(), 1, 1));
			table.addCell(getCell_Right(dwCount.getSF_GZ_TOTAL(), 1, 1));
			
			geShui_Count = gzmx.getDWRSCount_ByLB(month, "SELECT COALESCE(FORMAT(TO_NUMBER(SUM(B009)),2),'0') AS B009_SUM FROM CZGZ_ZGGZMX WHERE YF = '" + month +"'" );			
			geShui_Count = geShui_Count.trim();
			if(geShui_Count != null){
				table.addCell(getCell_Right((geShui_Count.equals("0.00") || geShui_Count.equals("0")) ? "" : geShui_Count, 1, 1));
			}else{
				table.addCell(getCell_Right("", 1, 1));
			}
			
			for(int i=0;i<listDW.size();i++){

				CZGZ_DWXX dw = listDW.get(i);
				
				table.addCell(getCell_Left(dw.getDW_MC(), 1, 1));
				table.addCell(getCell_Left("交通银行玄武支行", 1, 1));
				table.addCell(getCell_Left("320006610018010133201", 1, 1));
				table.addCell(getCell_Right(dw.getTF_RS(), 1, 1));
				table.addCell(getCell_Right(dw.getYF_GZ(), 1, 1));
				table.addCell(getCell_Right(dw.getDF_GZ(), 1, 1));
				table.addCell(getCell_Right(dw.getSF_GZ(), 1, 1));
				
				geShui_Count = "0";
				geShui_Count = gzmx.getDWRSCount_ByLB(month, "SELECT COALESCE(FORMAT(TO_NUMBER(SUM(B009)),2),'0') AS B009_SUM FROM CZGZ_ZGGZMX WHERE YF = '" + month +"' AND DW_DM = '" + dw.getDW_DM() + "'" );
				geShui_Count = geShui_Count.trim();
				if(geShui_Count != null){
					table.addCell(getCell_Right((geShui_Count.equals("0.00") || geShui_Count.equals("0")) ? "" : geShui_Count, 1, 1));
				}else{
					table.addCell(getCell_Right("", 1, 1));
				}
			}
			table.addCell(getBottomCell("银行付讫（盖章）：", 2, 1));
			table.addCell(getBottomCell("制表人：", 3, 1));
			table.addCell(getBottomCell("制表日期：", 3, 1));
			
			table.setWidthPercentage(99);// 设置表格宽度

	 	document.add(table);
	 	document.close();
	 } catch (Exception e) {
	 	e.printStackTrace();
	 	return -1;
		}
	 return 0;
	}
	private PdfPCell getCell(String cellValue, int colspan, int rowSpan) {
	 PdfPCell cell = new PdfPCell();
	 try {
	 cell = new PdfPCell(new Phrase(cellValue, FontChinese));
	 cell.setRowspan(rowSpan);
	 cell.setColspan(colspan);
	 //cell.setBorder(0);
	 cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	 } catch (Exception e) {
	 e.printStackTrace();
	 }
	 return cell;
	}
	
	private PdfPCell getCell_Right(String cellValue, int colspan, int rowSpan) {
	 PdfPCell cell = new PdfPCell();
	 try {
	 cell = new PdfPCell(new Phrase(cellValue, FontChinese));
	 cell.setRowspan(rowSpan);
	 cell.setColspan(colspan);
	 //cell.setBorder(0);
	 cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
	 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	 } catch (Exception e) {
	 e.printStackTrace();
	 }
	 return cell;
	}
	
	private PdfPCell getCell_Left(String cellValue, int colspan, int rowSpan) {
	 PdfPCell cell = new PdfPCell();
	 try {
	 cell = new PdfPCell(new Phrase(cellValue, FontChinese));
	 cell.setRowspan(rowSpan);
	 cell.setColspan(colspan);
	 //cell.setBorder(0);
	 cell.setHorizontalAlignment(Element.ALIGN_LEFT);
	 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	 } catch (Exception e) {
	 e.printStackTrace();
	 }
	 return cell;
	}
	
	private PdfPCell getBottomCell(String cellValue, int colspan, int rowSpan) {
	 PdfPCell cell = new PdfPCell();
	 try {
	 cell = new PdfPCell(new Phrase(cellValue, FontChinese));
	 cell.setRowspan(rowSpan);
	 cell.setColspan(colspan);
	 cell.setBorder(0);
	 cell.setHorizontalAlignment(Element.ALIGN_LEFT);
	 cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	 } catch (Exception e) {
	 e.printStackTrace();
	 }
	 return cell;
	}
	//设置页码
	public void setFooter(PdfWriter writer,BaseFont bf) throws DocumentException, IOException{
		int titleFontSize = 12;
		int presentFontSize = 8;
		//与页面大小相同      (595F, 1417F)
		PdfHeaderFooterDWGZ_ListAll headerFooter = new PdfHeaderFooterDWGZ_ListAll(bf,presentFontSize,new RectangleReadOnly(595F, 920F));
		//设置标题字体、内容、字体大小
		headerFooter.setTitleFontSize(titleFontSize);
		headerFooter.setFontTitle(new Font(bf, titleFontSize, Font.BOLD));
		headerFooter.setPageTitle(pageTitle);
		//设置内容页眉字体、内容 
		headerFooter.setFontDetail(new Font(bf, presentFontSize, Font.NORMAL));
		headerFooter.setPageHeader(pageHeader);
		
		writer.setPageEvent(headerFooter);
	}
	public static void main(String[] args) {
		//年月
		String month = "201905";
		//单位工资信息list
		List<CZGZ_DWXX> listDW=new ArrayList<CZGZ_DWXX>();
		GongziDWImpl gzDW = new GongziDWImpl();
		listDW = gzDW.listGZDWReport(month);
		
		//单位工资汇总
		CZGZ_DWXX_TOTAL dwCount=new CZGZ_DWXX_TOTAL();
		GongziDWImpl gzDWcount = new GongziDWImpl();
		dwCount = gzDWcount.getGZDWCount(month);
				
		System.out.println("listDW:" + listDW.size());
			
		CreateDWGZPDF_LISTALL c = new CreateDWGZPDF_LISTALL();
	 c.createPDF(listDW, dwCount, month);
	 System.out.println("制作完成");
	}
}
